package com.zygh.lz.controller.app;

import com.alibaba.fastjson.JSONObject;
import com.zygh.lz.dao.SectionMapper;
import com.zygh.lz.entity.Account;
import com.zygh.lz.entity.Register;
import com.zygh.lz.entity.Section;
import com.zygh.lz.entity.app.AppNote;
import com.zygh.lz.entity.app.AppTrailerInfo;
import com.zygh.lz.service.AccountService;
import com.zygh.lz.service.StaffService;
import com.zygh.lz.service.impl.AppTrailerInfoService;
import com.zygh.lz.stapprove.InBean;
import com.zygh.lz.stapprove.OutBean;
import com.zygh.lz.stapprove.OutBeanUtil;
import com.zygh.lz.stapprove.ViLog;
import com.zygh.lz.vo.ResultBean;
import com.zygh.lz.vo.ResultBeans;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * 省厅版没有用到此类
 */
@Api(tags = "APP登录", description = "AppLogin")
@RestController
public class AppLoginController {

    @Resource
    private StaffService staffService;
    @Resource
    private AppTrailerInfoService appTrailerInfoService;
    @Resource
    private SectionMapper sectionMapper;


    @ApiOperation("APP登录")
    @PostMapping("appLogin")
    @ViLog(logType = "0", module = "APP登录")
    public OutBean appLogin(@RequestBody(required = false) InBean inBean) {
        JSONObject jsonObject = inBean.convertObj(JSONObject.class);
        ResultBean resultBean = staffService.appLogin(jsonObject.getString("policeId"));
        return OutBeanUtil.resSuc(resultBean, inBean);
    }

    @ApiOperation("APP登出")
    @PostMapping("appLoginOut")
    @ViLog(logType = "5", module = "APP登出")
    public ResultBean appLoginOut(String IMEI, String user, HttpSession session) {
        session.setAttribute("policeNum", null);
        return staffService.appLoginOut(IMEI, user);
    }

    @ApiOperation("是否重新登录（顶掉）")
    @PostMapping("updateRegiser")
    @ViLog(logType = "6", module = "是否重新登录（顶掉）")
    public ResultBean updateRegiser(Register register) {
        return staffService.updateRegiser(register);
    }

    //    @GetMapping("modifuByPass")
//    @ViLog(logType = "3", module = "APP修改密码")
//    public ResultBean modifuByPass(Staff staff) {
//        return staffService.modifuByPass(staff);
//    }
    @Resource
    private AccountService accountService;

    @GetMapping("modifuByPass")
    @ViLog(logType = "3", module = "APP修改密码")
    public ResultBean modifuByPass(Account account) {
        return accountService.modifuByPass(account);
    }


    @PostMapping("modifu")
    @ViLog(logType = "3", module = "人员信息>修改")
    public OutBean modifu(@RequestBody(required = false) InBean inBean) {
        JSONObject jsonObject = inBean.convertObj(JSONObject.class);
        AppTrailerInfo appTrailerInfo = new AppTrailerInfo();
        appTrailerInfo.setId(Integer.valueOf(jsonObject.getString("id")));
        String driverState = jsonObject.getString("driverState");
         String rejectReason = jsonObject.getString("rejectReason");
        appTrailerInfo.setDriverState(Integer.valueOf(driverState));
        appTrailerInfo.setRejectReason(rejectReason);
        appTrailerInfo.setSignboard(0);
        ResultBeans resultBeans = appTrailerInfoService.updateTrailerInfo(appTrailerInfo);
        //修改时候如果通过新增发送短信
//        if ("2".equals(driverState)) {
//            System.out.println("状态：" + driverState);
//            AppTrailerInfo code = appTrailerInfoService.getById(appTrailerInfo.getId());
//            //新增需发送短信的信息
//            AppNote appNote = new AppNote();
//            appNote.setCode(code.getCode());
//            appNote.setPlate(code.getVehiclePlate());
//            appNote.setPlace(code.getIllegalAddress());
//            appNote.setAddress(code.getParkPlace() + appTrailerInfoService.getAddress(code.getParkPlace()));
//
//            //咨询电话
//            Section section = sectionMapper.selectByPrimaryKey(code.getSectionId());
//            appNote.setContact(section.getHotline());
//            appNote.setPhone(code.getPhone());
//            appNote.setCreatTime(new Date());
//            appNote.setStatus(0);
//            appTrailerInfoService.insertNote(appNote);
//        }


        return OutBeanUtil.resSuc(resultBeans, inBean);
    }

}
